package com.magina.antiPro.component;

import com.google.common.base.Preconditions;
import com.magina.antiPro.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Future;

@Component
@Slf4j
public class AsyncTask {

    @Async
    public Future<String> task1() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(10000);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return new AsyncResult<String>("task1执行完毕");
    }

    @Async
    public Future<String> task2() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(20000);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return new AsyncResult<String>("task2执行完毕");
    }
    @Async
    public Future<String> task3_0() throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(2000);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("task3_0:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return new AsyncResult<String>("task3_0执行完毕");
    }
    @Async
    public Future<String> task3() throws InterruptedException, BusinessException {
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(2222);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("进入task3");
        if(true){
            throw new BusinessException("测试业务异常(有返回值)！");
        }
        System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");

        return new AsyncResult<String>("task3执行完毕");
    }

    @Async
    public void taskException1() throws InterruptedException, BusinessException {
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(2000);
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("taskException1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        if(true){
            throw new BusinessException("测试业务异常(wu返回值)！");
        }
    }

    @Async
    public void taskException2() throws InterruptedException, BusinessException {
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(3000);
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("taskException1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");

        Preconditions.checkArgument(false,"测试业务异常(wu返回值)2！");
    }


    @Async
    public Future<Object> testSameDataChange1(List<Integer> testList) throws InterruptedException, BusinessException {
        try{
            long currentTimeMillis = System.currentTimeMillis();
            Thread.sleep(2000);
            List<Integer> list1 = Lists.newArrayList();
            list1.add(111);
            list1.remove(111);
            testList.addAll(list1);
            System.out.printf("testSameDataChange1: %s %n",testList);
            long currentTimeMillis1 = System.currentTimeMillis();
            log.info("testSameDataChange1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        }catch (Exception e){
            if(e instanceof  BusinessException){
                throw new BusinessException(e.getMessage());
            }
            throw new BusinessException(e.getMessage());
        }
        return new AsyncResult<Object>(testList);
    }

    @Async
    public Future<Object> testSameDataChange2(List<Integer> testList) throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(1000);
        List<Integer> list2 = Lists.newArrayList();
        list2.add(222);
        testList.addAll(list2);
        testList.remove(0);
        System.out.printf("testSameDataChange2: %s %n",testList);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("testSameDataChange2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return new AsyncResult<Object>(testList);
    }
    @Async
    public Future<Object> testSameDataChange3(List<Integer> testList) throws InterruptedException{
        long currentTimeMillis = System.currentTimeMillis();
        Thread.sleep(4000);
        List<Integer> list3 = Lists.newArrayList();
        list3.add(333);
        testList.add(333);
        System.out.printf("testSameDataChange3: %s %n",testList);
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("testSameDataChange3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return new AsyncResult<Object>(testList);
    }

}

